import 'package:flutter/material.dart';
/// 由于用了flutterBoost，所以不能用flutter原始的方式监听生命周期
class LifecyclePage extends StatefulWidget {
  const LifecyclePage({super.key});

  @override
  State<LifecyclePage> createState() => _LifecyclePageState();
}

class _LifecyclePageState extends State<LifecyclePage> with WidgetsBindingObserver {
  late final AppLifecycleListener _listener;
  String _currentState = '未初始化';
  final List<String> _stateLog = [];

  @override
  void initState() {
    super.initState();
    
    _listener = AppLifecycleListener(
      onStateChange: _onStateChange,
      onResume: _onResume,
      onPause: _onPause,
      onHide: _onHide,
      onShow: _onShow,
      onInactive: _onInactive,
      onRestart: _onRestart,
      onDetach: _onDetach,
    );
    
    WidgetsBinding.instance.addObserver(this);
  }

  void _onStateChange(AppLifecycleState state) {
    setState(() {
      _currentState = state.toString();
      _stateLog.add('${DateTime.now().toIso8601String()}: State changed to $state');
      if (_stateLog.length > 20) _stateLog.removeAt(0); // 限制日志数量
    });
    debugPrint('LifecyclePage: AppLifecycleState changed to $state');
  }

  void _onResume() {
    debugPrint('LifecyclePage: App resumed');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App resumed');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onPause() {
    debugPrint('LifecyclePage: App paused');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App paused');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onHide() {
    debugPrint('LifecyclePage: App hidden');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App hidden');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onShow() {
    debugPrint('LifecyclePage: App shown');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App shown');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onInactive() {
    debugPrint('LifecyclePage: App inactive');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App inactive');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onRestart() {
    debugPrint('LifecyclePage: App restarted');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App restarted');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  void _onDetach() {
    debugPrint('LifecyclePage: App detached');
    setState(() {
      _stateLog.add('${DateTime.now().toIso8601String()}: App detached');
      if (_stateLog.length > 20) _stateLog.removeAt(0);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AppLifecycleListener 测试'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '当前状态: $_currentState',
              style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 20),
            const Text(
              '状态日志:',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 10),
            Expanded(
              child: ListView.builder(
                itemCount: _stateLog.length,
                itemBuilder: (context, index) {
                  return Card(
                    child: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(_stateLog[index]),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}